package com.ethannhu.todaynews.fragment

import com.ethannhu.superui.extension.longToast
import com.ethannhu.todaynews.AppContext
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.activity.BaseLogicActivity
import com.ethannhu.todaynews.component.login.LoginHomeActivity
import com.ethannhu.todaynews.entity.response.BaseResponse
import com.ethannhu.todaynews.model.BaseViewModel
import com.ethannhu.todaynews.util.PreferenceUtil

abstract class BaseLogicFragment : BaseCommonFragment() {
    /**
     * 获取界面方法
     */
    protected val hostActivity: BaseLogicActivity
        get() = requireActivity() as BaseLogicActivity

    /**
     * 初始化通用 ViewModel逻辑
     */
    protected fun initViewModel(viewModel: BaseViewModel) {
        //关闭界面
        viewModel.finishPage.observe(this) {
            requireActivity().finish()  //只有在 it的值被显式更新的时候，才会触发
        }
        //本地提示
        viewModel.tip.observe(this) {
            onTip(it)
        }
        //异常
        viewModel.exception.observe(this) {
            onException(it)
        }
        //网络响应业务失败
        viewModel.response.observe(this) {
            onResponse(it)
        }
        //加载提示
        viewModel.loading.observe(this) {

        }
    }

    open fun onTip(data: Int) {
        hostActivity.onTip(data)
        onError()


    }

    open fun onException(data: Throwable) {
        hostActivity.onException(data)
        onError()
    }

    open fun onResponse(data: BaseResponse) {
        hostActivity.onResponse(data)
        onError()
    }

    open fun onError() {

    }

    /**
     * 只要用户登录了，才会执行代码块
     *
     * @param data
     */
    fun loginAfter(data: Runnable) {
        hostActivity.loginAfter(data)
    }
    fun toLogin() {
        hostActivity.toLogin()
    }
}